﻿using System;
using MSAcademic.v2.Infrastructure.AccessRights;
using MSAcademic.v2.Infrastructure.Article;
using MSAcademic.v2.Infrastructure.Login;
using MSAcademic.v2.Infrastructure.User;

namespace MSAcademic.v2.Web.Areas.Admin.Models.Article
{
    public partial class ArticlePreviewModel
    {
        public int Id { get; set; }
    }

    public partial class ArticlePreviewModel
    {
        public IUser Author
        {
            get
            {
                if (_author == null)
                {
                    _author = new Lazy<IUser>(() => _userQueries.GetArticleAuthor(Id));
                }
                return _author.Value;
            }
        }
        private Lazy<IUser> _author;
        public IArticle Article
        {
            get
            {
                if (_article == null)
                {
                    _article = new Lazy<IArticle>(() => _articleQueries.GetArticle(Id));
                }
                return _article.Value;
            }
        }
        private Lazy<IArticle> _article;
    }
    public partial class ArticlePreviewModel
    {
        public IUser SignedInUser
        {
            get
            {
                if (_signedInUser == null)
                {
                    _signedInUser = new Lazy<IUser>(() => _loginService.TryGetSignedInUser());
                }
                return _signedInUser.Value;
            }
        }

        private Lazy<IUser> _signedInUser;
        public bool CanUpdate
        {
            get
            {
                if (_canUpdate == null)
                {
                    _canUpdate = new Lazy<bool>(() => this.SignedInUser != null && _accessRightsService.CanUserAccess(this.SignedInUser, this.Article.UserId, AccessRight.CanUpdate));
                }
                return _canUpdate.Value;
            }
        }
        private Lazy<bool> _canUpdate;
    }
    public partial class ArticlePreviewModel
    {
        private readonly IUserQueries _userQueries;
        private readonly IArticleQueries _articleQueries;
        private readonly ILoginService _loginService;
        private readonly IAccessRightsService _accessRightsService;
        public ArticlePreviewModel(IUserQueries userQueries,
                                   IArticleQueries articleQueries,
                                   ILoginService loginService,
                                   IAccessRightsService accessRightsService)
        {
            _userQueries = userQueries;
            _articleQueries = articleQueries;
            _loginService = loginService;
            _accessRightsService = accessRightsService;
        }
    }
}